// pages/changeuserinfo/changeuserinfo.js
import {
  getUserInfo,
  editUserInfo,
  exitLogin,
  login
} from '../../utils/networkRequest.js'
import {
  errorMsg
} from '../../utils/tool.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dataInfo: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let self = this;
    getUserInfo((r) => {
      if (r.code == 0) {
        self.setData({
          dataInfo: r.data
        })
      } else {
        errorMsg(r.message);
      }
    })
  },
  //获取发件人名称
  getName: function(e) {
    this.setData({
      ["dataInfo.name"]: e.detail.value
    })
  },
  //获取发件人电话
  getPhone: function(e) {
    this.setData({
      ["dataInfo.mobile"]: e.detail.value
    })
  },
  //获取发件人地址
  getAddress: function(e) {
    this.setData({
      ["dataInfo.address"]: e.detail.value
    })
  },
  //获取团队名称
  getTeamName: function(e) {
    this.setData({
      ["dataInfo.team_name"]: e.detail.value
    })
  },
  //获取售后地址
  getAFAddress:function(e){
    this.setData({
      ["dataInfo.af_address"]: e.detail.value
    })
  },
  //获取售后备注
  getAFmemo:function(e){
    this.setData({
      ["dataInfo.af_memo"]: e.detail.value
    })
  },  
  formSubmit: function(e) {
    let dataInfo = e.detail.value;
    editUserInfo(dataInfo.name, dataInfo.mobile, dataInfo.address, dataInfo.team_name, dataInfo.af_address, dataInfo.af_memo, e.detail.formId, (r) => {
      if (r.code == 0) {
        errorMsg(r.message)
      } else {
        errorMsg(r.message)
      }
    })
  },
  phoneLogin: function () {
    let self = this; 
    wx.getUserInfo({
      withCredentials: true,
      success: function (res) { 
        // 登录 
        wx.login({
          success: lr => {
            // 发送 res.code 到后台换取 openId, sessionKey, unionId
            login(lr.code, res.iv, res.encryptedData, res.userInfo, (r) => {
              if (r.code == 0) { 
                wx.navigateTo({
                  url: '../phoneLogin/phoneLogin?token=' + r.data.token,
                })  
              } else {
                errorMsg(r.message);
              }
            })
          }
        })  
      }, 
    })
  },

  goDataMove: function() {
    wx.navigateTo({
      url: '../datamove/datamove'
    })
  },
  //点击退出手机号登录
  onExitLogin: function() {
    exitLogin((r) => {
      if (r.code == 0) {
        wx.reLaunch({
          url: '../about/about',
        })
        errorMsg(r.message)

      } else {
        errorMsg(r.message)
      }
    })
  }

})